package lpl.com.xyd.test.manage.impl;

import lpl.com.xyd.test.manage.common.response.HttpResponse;
import lpl.com.xyd.test.manage.convert.WorkPlanConvert;
import lpl.com.xyd.test.manage.domain.WorkPlanDomain;
import lpl.com.xyd.test.manage.facade.WorkPlanFacade;
import lpl.com.xyd.test.manage.factory.WorkPlanDomainFactory;
import lpl.com.xyd.test.manage.info.WorkPlanInfo;
import lpl.com.xyd.test.manage.order.WorkPlanAddOrder;
import lpl.com.xyd.test.manage.order.WorkPlanMyOrder;
import lpl.com.xyd.test.manage.order.WorkPlanQueryOrder;
import lpl.com.xyd.test.manage.param.WorkPlanParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WorkPlanFacadeImpl implements WorkPlanFacade {

    @Autowired
    private WorkPlanDomainFactory workPlanDomainFactory;

    @Override
    public HttpResponse<WorkPlanInfo> addWorkPlan(WorkPlanAddOrder order) {
        HttpResponse<WorkPlanInfo> response = new HttpResponse<>();
        WorkPlanParam param = new WorkPlanParam();
        BeanUtils.copyProperties(order,param);
        WorkPlanDomain domain = workPlanDomainFactory.create(param);
        boolean result = workPlanDomainFactory.store(domain);
        if(!result){
            response.failfure();
            response.setMessage("保存失败！");
            return response;
        }
        response.successful();
        response.setMessage("保存成功！");
        return response;
    }

    @Override
    public HttpResponse<WorkPlanInfo> query(WorkPlanQueryOrder order) {
        return null;
    }

    @Override
    public HttpResponse<List<WorkPlanInfo>> queryMy(WorkPlanMyOrder order) {
        HttpResponse<List<WorkPlanInfo>> response = new HttpResponse<>();
        WorkPlanParam param = new WorkPlanParam();
        param.setUserId(order.getUserId());
        List<WorkPlanDomain> domains = workPlanDomainFactory.activeList(param);
        if (null == domains || domains.size() <= 0){
            response.failfure();
            response.setMessage("没有数据");
        }

        List<WorkPlanInfo> infos = new ArrayList<>(domains.size());
        domains.forEach(v->{
            WorkPlanInfo info = WorkPlanConvert.convert(v);
            infos.add(info);
        });
        response.successful();
        response.setData(infos);
        return response;
    }
}
